#include <gtest/gtest.h>
#include "cstring.h"
#include "shared_ptr.h"
#include "double_buffer.h"
#include "split.h"

TEST(CString, test) {
    CString s1("hello");
    EXPECT_EQ(5, s1.length());
    EXPECT_STREQ("hello", s1.data());
    
    CString s2 = "zhb";
    EXPECT_STREQ("zhb", s2.data());

    CString s3 = s1;
    EXPECT_STREQ("hello", s3.data());
    EXPECT_EQ('e', s3[1]);
    
    EXPECT_STREQ("hellozhb", (s1 + s2).data());
    EXPECT_STREQ("hellozhb", (s1 + "zhb").data());
    EXPECT_STREQ("hellohello", s1.append(s3).data());
    EXPECT_STREQ("hellohellozhb", (s1.append("zhb")).data());
    std::cout << s1 + "233434" << std::endl;
}

TEST(SharedPtr, test) {
    int* pi = new int(10);
    {
        SharedPtr<int> p(pi);
        EXPECT_EQ(1, p.use_count());

        SharedPtr<int> p1 = p;
        EXPECT_EQ(1, p.use_count());
        EXPECT_EQ(2, p1.use_count());

        SharedPtr<int> p2(p1);
        EXPECT_EQ(2, p1.use_count());
        EXPECT_EQ(3, p2.use_count());
        
        int* pi1 = new int(20);
        SharedPtr<int> p3(pi1);
        EXPECT_EQ(1, p3.use_count());
    }
}


TEST(DoubleBuffer, test) {
    DoubleBuffer db;
    EXPECT_STREQ("init data", db.get().data());
    db.update();
    EXPECT_STREQ("new data", db.get().data());
}

TEST(Split, test) {
    std::string s = "hello,world";
    auto v = split(s, ',');
    EXPECT_EQ(2, v.size());
    EXPECT_STREQ("hello", v[0].c_str());
    EXPECT_STREQ("world", v[1].c_str());

    s = "1";
    v = split(s, ',');
    EXPECT_EQ(1, v.size());
    EXPECT_STREQ("1", v[0].c_str());
}
